package fictional.GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 * A popup which shows enlarged images.
 */
@SuppressWarnings("serial")
public class ViewImagePopup extends JFrame
{
	JLabel imageLabel;	
	
	public ViewImagePopup(String imgLink)
	{		
		this.setSize(new Dimension(500, 600));
        //this.setLocationRelativeTo(null);
        this.setBackground(Color.white);
        this.setTitle("View Image");
		
		JPanel imagePanel = new JPanel();
		imagePanel.setLayout(new GridBagLayout());
		imageLabel = new javax.swing.JLabel();
		imagePanel.add(imageLabel);
		JScrollPane imageScrollPane = new JScrollPane();
		imageScrollPane.setViewportView(imagePanel);
		getContentPane().add(imageScrollPane, BorderLayout.CENTER);
		
		GetImage getImage = new GetImage(imgLink);
		Thread drawImageThread = new Thread(getImage);
		drawImageThread.run();	
	}
	
	private class GetImage implements Runnable
	{

		String imgLink;
		
		public GetImage(String imgLink)
		{
			this.imgLink = imgLink;
		}
		
		@Override
		public void run() {
			
			URL imgURL = null;
			
			try {
				imgURL = new java.net.URL(imgLink);
			} catch (MalformedURLException e) {
			       JLabel label = new JLabel("Error loading image.");
			       getContentPane().add(label, BorderLayout.CENTER);
			       return;
			}
			
			ImageIcon imageIcon = new ImageIcon(imgURL, "image");
			imageLabel.setIcon(imageIcon);
			ViewImagePopup.this.setSize(imageIcon.getIconWidth() + 40, imageIcon.getIconHeight() + 60);
			ViewImagePopup.this.repaint();
		}
	}
}

